from cloudcontrol._users import User

class Role(object):
    """a collection of User objects that can be treated as
    if it were a user itself.  Return values for all member
    functions will return tuples that map a user"""
    
    def __init__(self, *userlist):
        
        self.__users = []
        for user in userlist:
            
            # force each element that was declared to become a User object
            if not isinstance(user, User):
                user = User(user)
            
            # keep track of all users
            self.__users.append(user)
    
    def configure(self, *args, **kwargs):
        """configures every User in this Role with the given config"""
        for user in self.__users:
            user.configure(*args, **kwargs)
    
    def run(self, *args, **kwargs):
        """runs the command on all contained Users"""
        response = None
        for user in self.__users:
            user_response = user.run(*args, **kwargs)
            if not response:
                response = user_response
            else:
                response.update(user_response)
        return response
    
    def expect_input_for(self, *args, **kwargs):
        """asks the user for input when `pattern` is recognized in the output stream"""
        for user in self.__users:
            user.expect_input_for(*args, **kwargs)
    
    def expect_password_for(self, *args, **kwargs):
        """asks the user for a password when `pattern` is recognized in the output stream.
        This is different from `expect_input_for` in that logging methods will
        echo the response with asterisks rather than plaintext (to protect
        the secrecy of the password in the logs)"""
        for user in self.__users:
            user.expect_password_for(*args, **kwargs)
    
    def respond_with_input_for(self, *args, **kwargs):
        """responds with plaintext input when `pattern` is recognized in the output stream"""
        for user in self.__users:
            user.respond_with_input_for(*args, **kwargs)
    
    def respond_with_password_for(self, *args, **kwargs):
        """responds with a password when `pattern` is recognized in the output stream.
        This is different from `respond_with_input_for` in that logging methods will
        echo the response with asterisks rather than plaintext (to protect
        the secrecy of the password in the logs)"""
        for user in self.__users:
            user.respond_with_password_for(*args, **kwargs)
